home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / beddow / netapi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-04  |  5.4 KB  |  195 lines

  1. /* ******************************************************************** */
  2. /* Netbios API related functions                                               */
  3.  
  4. /* **********************************************  */
  5. /* NetBIOS call via 0x5C interrupt with auto retry */
  6. net_bios_call(ncbptr)
  7. struct Ncb *ncbptr ;
  8. { struct SREGS SegRegs ;
  9.   union REGS InRegs, OutRegs ;
  10.   struct Ncb  far *l_ptr ;
  11.  
  12.   l_ptr = (struct Ncb far *) ncbptr ;
  13.   segread(&SegRegs) ;
  14.   SegRegs.es = FP_SEG(l_ptr) ;
  15.   InRegs.x.bx = FP_OFF(l_ptr) ;
  16.   InRegs.x.ax = 0x400 ;
  17.  
  18.   int86x(0x5c, &InRegs, &OutRegs, &SegRegs) ;
  19.  
  20.   return(OutRegs.h.al) ;
  21. }
  22.  
  23. /* Check for netbios : return FALSE if found */
  24. chk_net_bios()
  25. { struct SREGS SegRegs ;
  26.   union REGS InRegs, OutRegs ;
  27.   struct Ncb  far *l_ptr ;
  28.   struct Ncb  test_ncb ;
  29.  
  30. /* First check Int 0x5c is loaded */
  31.   InRegs.x.ax = 0x355c ;        
  32.   int86x( 0x21, &InRegs, &OutRegs, &SegRegs) ;
  33.   if (SegRegs.es == 0 || SegRegs.es == 0xf000)
  34.     return(1) ;
  35.  
  36. /* Make an illegal Ncb request to double check */
  37.   memset(&test_ncb, 0, sizeof(struct Ncb)) ;
  38.   test_ncb.NCB_command = 0x7f ;
  39.   l_ptr = (struct Ncb far *) &test_ncb ;
  40.   segread(&SegRegs) ;
  41.   SegRegs.es = FP_SEG(l_ptr) ;
  42.   InRegs.x.bx = FP_OFF(l_ptr) ;
  43.   InRegs.x.ax = 0 ;
  44.  
  45.   int86x(0x5c, &InRegs, &OutRegs, &SegRegs) ;
  46.  
  47.   if (OutRegs.h.al)
  48.     return(0) ;
  49.   else
  50.     return(1) ;  
  51. }
  52.  
  53. /* Get Net bios error message */
  54. char *get_neterrmess(int error)
  55. { static char *mess1 = "Command not complete" ;
  56.   static char *mess2 = "Reply timeout" ;
  57.   static char *mess3 = "Hardware error" ;
  58.   static char *mess4 ="" ;
  59.   
  60.   if (error == 0xff)
  61.     return(mess1) ;
  62.   if (error == NETTIMEOUTERR)
  63.     return(mess2) ;
  64.   if (error >= 0x40 && error <= 0xfe)
  65.     return(mess3) ;
  66.   if (error <= 0x26)
  67.     return(neterrmess[error]) ;
  68.   return(mess4) ;
  69. }
  70.  
  71. /* ************************ */
  72. /* Handle the name commands */
  73. net_name_command(int command, char *name, PROC post, struct Ncb *ncbptr)
  74.    init_ncb(ncbptr) ;
  75.  
  76.    if (post == NULL)
  77.      ncbptr->NCB_command = command ;
  78.    else
  79.      ncbptr->NCB_post = (FARPROC)post ;
  80.    ncbptr->NCB_lana_num = 0 ;
  81.    set_ncb_name(name, ncbptr) ;
  82.     
  83.    return(net_bios_call(ncbptr)) ;
  84. }
  85.  
  86. /* **************************** */
  87. /* Handle the datagram commands */
  88. net_dgram_command(int command, int num, char *callname, void *buf, int buflen,
  89.                 FARPROC post, struct Ncb *ncbptr)
  90.    init_ncb(ncbptr) ;
  91.    ncbptr->NCB_command = command ;
  92.    ncbptr->NCB_post = post ;
  93.    ncbptr->NCB_lana_num = 0 ;
  94.    if (callname != NULL)
  95.      set_ncb_callname(callname, ncbptr) ;
  96.    ncbptr->NCB_num = num ;
  97.    ncbptr->NCB_buffer = (char far *)buf ;
  98.    ncbptr->NCB_length = buflen ;    
  99.  
  100.    return(net_bios_call(ncbptr)) ;
  101. }
  102.  
  103. /* ************** */
  104. /* Cancel command */
  105. net_command_cancel(struct Ncb *ncbcancelptr, struct Ncb *ncbptr)
  106. {  init_ncb(ncbptr) ;
  107.    ncbptr->NCB_command = NCBCANCEL ;
  108.    ncbptr->NCB_lana_num = 0 ;
  109.    ncbptr->NCB_buffer = (char far *)ncbcancelptr ;
  110.    ncbcancelptr->NCB_post = NULL ;
  111.    return(net_bios_call(ncbptr)) ;
  112. }
  113.  
  114.  
  115.  
  116. /* ****************************************************** */
  117. /* Utility functions                                      */
  118.  
  119. /* ********************************** */
  120. /* Initialise a Netbios control block (NCB) */
  121. init_ncb(struct Ncb *ncbptr)
  122. { int cnt = sizeof(struct Ncb) ;
  123.   char *c = (char *)ncbptr ;
  124.   while(cnt)
  125.     { *c++ = 0 ;
  126.       cnt-- ;
  127.     }
  128. }
  129.  
  130. /* ************************************************************************** */
  131. /* Copy a name string into an NCB name and pad string with spaces to 16 chars */
  132. set_ncb_name(char *name, struct Ncb *ncbptr)
  133. { int i ;
  134.   char *s ;
  135.  
  136.   strncpy((char *)ncbptr->NCB_name, name, 16) ;
  137.   s = (char *)(ncbptr->NCB_name) + strlen(name) ;
  138.   for (i = strlen(name); i < 16; i++)
  139.    *s++ = ' ' ;
  140. }
  141.  
  142. /* ******************************************************************************* */
  143. /* Copy a name string into an NCB call name and pad string with spaces to 16 chars */
  144. set_ncb_callname(char *name, struct Ncb *ncbptr)
  145. { int i ;
  146.   char *s ;
  147.  
  148.   strncpy((char *)ncbptr->NCB_callname, name, 16) ;
  149.   s = (char *)(ncbptr->NCB_callname) + strlen(name) ;
  150.   for (i = strlen(name); i < 16; i++)
  151.      *s++ = ' ' ;
  152. }
  153.  
  154. /* ********************************************************************* */
  155. /* Copy a NCB call name to a string, removing spaces at end of call name */
  156. char *ret_ncb_callname(char *name, struct Ncb *ncbptr)
  157. { int i = 16 ;
  158.   unsigned char *s = ncbptr->NCB_callname ;
  159.   
  160.   while (i-- && *(s + i) == ' ') ;
  161.   i++ ;
  162.   strncpy(name, (char *)ncbptr->NCB_callname, i) ;
  163.   *(name + i) = 0 ;
  164.  
  165.   return(name) ;
  166. }   
  167.  
  168.  
  169.  
  170. /* *********************************************************** */
  171. /* Net name list search functions */
  172.  
  173. /* ******************************************************** */
  174. /* Search the net name descriptor list for an entry by name */
  175. struct netrpcserver *netname(char *name)
  176. {  struct netrpcserver *srchptr ;
  177.  
  178.    srchptr = netrpcserverlist ;
  179.    while (strcmp(srchptr->name, name) && (srchptr = srchptr->prev) != NULL) ;
  180.    return(srchptr) ;
  181. }
  182.  
  183. /* ****************************************************** */
  184. /* Search the net name descriptor list for an entry by id */
  185. struct netrpcserver *netid(int id)
  186. {  struct netrpcserver *srchptr ;
  187.  
  188.    srchptr = netrpcserverlist ;
  189.    while (srchptr->id != id && (srchptr = srchptr->prev) != NULL) ;
  190.    return(srchptr) ;
  191. }
  192.  
  193.